package com.jbh.netlibrary.r;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.jbh.netlibrary.i.NetStateListener;
import com.jbh.netlibrary.m.NetManager;
import com.jbh.netlibrary.u.NetStateUtils;
import com.jbh.netlibrary.u.ForActivityUtil;
import com.jbh.netlibrary.v.NetErrorView;

public class NetWorkStateReceiver extends BroadcastReceiver {

    private NetErrorView errorView;
    private NetStateListener netStateListener;

    public NetWorkStateReceiver(NetStateListener netStateListener) {
        this.netStateListener = netStateListener;
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        boolean b = NetStateUtils.isHaveNet(context);
        ConnectivityManager c = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Log.v("net_api", "[net] - > " + (b ? "网络正常可用" : "网络不可用"));
        isShowNotNet(context, b);
    }


    /**
     * 显示没有网络的view逻辑
     *
     * @param b true表示网络连接正常
     */
    public void isShowNotNet(Context context, boolean b) {
        if (b) {
            dismissNetView(context);
        } else {
            show(context);
        }
    }


    private AlertDialog alertDialog;

    public void show(final Context context) {
//        showMsgDialog(context);
        showNewView(context);
    }



    public void showNewView(final Context context) {
        AppCompatActivity activity = ForActivityUtil.getAppCompatActivity(context);
        if (activity != null) {
            if (NetManager.getInstance().config != null && NetManager.getInstance().config.getNoNetView() != null) {
                addChildView(context,NetManager.getInstance().config.getNoNetView());
            } else {
                errorView = new NetErrorView(context);
                addChildView(context,errorView);
                errorView.setOnRefreshListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (NetStateUtils.isHaveNet(context)) {
                            dismissNetView(context);
                        } else {
                            Toast.makeText(context, "网络断开，暂时无法刷新！", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }
    }




    /**
     * 添加view
     * @param context
     * @param view
     */
    void addChildView(Context context,View view){
        AppCompatActivity a = ForActivityUtil.getAppCompatActivity(context);
        if (a!=null && !a.isFinishing()){
            FrameLayout mDecorView = (FrameLayout) ForActivityUtil.getAppCompatActivity(context).getWindow().getDecorView();
            mDecorView.removeView(view);
            mDecorView.addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }



    void removeChildView(Context context,View view){
        AppCompatActivity a = ForActivityUtil.getAppCompatActivity(context);
        if (a!=null && !a.isFinishing()){
            FrameLayout mDecorView = (FrameLayout) ForActivityUtil.getAppCompatActivity(context).getWindow().getDecorView();
            mDecorView.removeView(view);
        }
    }





    public void dismissNetView(Context context) {
        AppCompatActivity activity = ForActivityUtil.getAppCompatActivity(context);
        if (activity != null) {
            if (NetManager.getInstance().config != null && NetManager.getInstance().config.getNoNetView() != null) {
                removeChildView(context,NetManager.getInstance().config.getNoNetView());
            } else {
                if (errorView != null) {
                    removeChildView(context,errorView);
                    errorView = null;
                }
            }
            if (netStateListener != null) {
                netStateListener.onRefresh();
            }
        }
        if (alertDialog != null) {
            alertDialog.dismiss();
        }
    }
}